/**
 * @file main.c
 * @brief 
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2024-11-23
 * 
 * @copyright Copyright (c) 2024  by xuesong
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-11-23     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */
#define FDB_LOG_TAG "[main]"
#include <stdio.h>
#include <flashdb.h>

#define FDB_LOG_TAG "[main]"

// static pthread_mutex_t kv_locker, ts_locker;
// static pthread_mutexattr_t kv_locker_attr, ts_locker_attr;
static uint32_t boot_count = 0;
static time_t boot_time[10] = {0, 1, 2, 3};
/* default KV nodes */
static struct fdb_default_kv_node default_kv_table[] = {
        {"username", "armink", 0}, /* string KV */
        {"password", "123456", 0}, /* string KV */
        {"boot_count", &boot_count, sizeof(boot_count)}, /* int type KV */
        {"boot_time", &boot_time, sizeof(boot_time)},    /* int array type KV */
};
/* KVDB object */
static struct fdb_kvdb xKvdb = { 0 };
/* TSDB object */
struct fdb_tsdb xTsdb = { 0 };
/* counts for simulated timestamp */
static int counts = 0;

extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_string_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_blob_sample(fdb_kvdb_t kvdb);
extern void tsdb_sample(fdb_tsdb_t tsdb);

static void lock(fdb_db_t db)
{
    // pthread_mutex_lock((pthread_mutex_t *)db->user_data);
}

static void unlock(fdb_db_t db)
{
    // pthread_mutex_unlock((pthread_mutex_t *)db->user_data);
}

static fdb_time_t get_time(void)
{
    static time_t t = 0;
    t++;
    return t;
    // return time(NULL);
}

int main(void)
{
	struct fdb_default_kv default_kv;

	default_kv.kvs = default_kv_table;
	default_kv.num = 
		sizeof(default_kv_table) / sizeof(default_kv_table[0]);

	/* Printf save block information. */
	FDB_INFO("%s:fdb_tsdb size:%d \r\n" , __FUNCTION__ , sizeof(struct fdb_tsdb) );
	FDB_INFO("%s:fdb_kvdb size:%d \r\n" , __FUNCTION__ , sizeof(struct fdb_kvdb) );

    printf("test FlashDB\r\n");
    fullTestFlashBin();

	/* Clear data. */
	memset( &xTsdb, 0, sizeof(struct fdb_tsdb) );
	memset( &xKvdb, 0, sizeof(struct fdb_kvdb) );

	/* KVDB module initialization */
	fdb_kvdb_init(&xKvdb , "User" , "kvdb1", &default_kv , NULL);
	
	/* time series initialize storage */
	fdb_tsdb_init(&xTsdb , "HsRec" ,"tsdb1", (fdb_get_time)get_time , 64 , NULL);

	while(1)
	{
		kvdb_basic_sample(&xKvdb);
		kvdb_type_string_sample(&xKvdb);
		kvdb_type_blob_sample(&xKvdb);
		tsdb_sample(&xTsdb);
	}
    return 0;
}